package com.ruoyi.develop.goodsSampleApply.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import com.ruoyi.develop.goodsSale.mapper.GoodsSaleMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.mapper.LvRukumingxiMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsSampleApply.mapper.GoodsSampleApplyMapper;
import com.ruoyi.develop.goodsSampleApply.domain.GoodsSampleApply;
import com.ruoyi.develop.goodsSampleApply.service.IGoodsSampleApplyService;

/**
 * 质检样品申请Service业务层处理
 *
 * @author qh
 * @date 2025-02-15
 */
@Service
public class GoodsSampleApplyServiceImpl implements IGoodsSampleApplyService {
    @Resource
    private GoodsSampleApplyMapper goodsSampleApplyMapper;
    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;

    @Resource
    private LvRukumingxiMapper lvRukumingxiMapper;


    /**
     * 查询质检样品申请
     *
     * @param id 质检样品申请主键
     * @return 质检样品申请
     */
    @Override
    public GoodsSampleApply selectById(String id) {
        return goodsSampleApplyMapper.selectById(id);
    }

    /**
     * 查询质检样品申请列表
     *
     * @param entity 质检样品申请
     * @return 质检样品申请
     */
    @Override
    public List<GoodsSampleApply> selectList(GoodsSampleApply entity) {
        return goodsSampleApplyMapper.selectList(entity);
    }

    /**
     * 新增质检样品申请
     *
     * @param entity 质检样品申请
     * @return 结果
     */
    @Override
    public int insert(GoodsSampleApply entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if (StringUtils.isNotBlank(entity.getGoodsId())) {
            GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
            if (goodsOrderIn != null) {
                entity.setGoodsCode(goodsOrderIn.getCode());
                entity.setGoodsName(goodsOrderIn.getName());
                entity.setGoodsModel(goodsOrderIn.getModel());
            }
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsSampleApplyMapper.insert(entity);
    }

    /**
     * 修改质检样品申请
     *
     * @param entity 质检样品申请
     * @return 结果
     */
    @Override
    public int update(GoodsSampleApply entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        if (StringUtils.isNotBlank(entity.getGoodsId())) {
            GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
            if (goodsOrderIn != null) {
                entity.setGoodsCode(goodsOrderIn.getCode());
                entity.setGoodsName(goodsOrderIn.getName());
                entity.setGoodsModel(goodsOrderIn.getModel());
            }
        }
        return goodsSampleApplyMapper.update(entity);
    }


    @Override
    public int assess(GoodsSampleApply entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        // 如果出库 审核通过 减库存 增加出库明细
        if (BusinessConstants.SAMPLE_ASSESS_STATUS_OUT_PASS.equals(entity.getStatus())) {
            if(StringUtils.isNotBlank(entity.getGoodsId())){
                GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
                goodsOrderIn.setCount(goodsOrderIn.getCount().subtract(entity.getCount()));
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().subtract(entity.getCount()));
                // 插入出库记录
                LvRukumingxi lvRukumingxi = new LvRukumingxi();
                lvRukumingxi.setId(IdUtils.fastUUID());
                lvRukumingxi.setGoodsId(goodsOrderIn.getId());
                lvRukumingxi.setCode(goodsOrderIn.getCode());
                lvRukumingxi.setUnit(goodsOrderIn.getUnit());
                lvRukumingxi.setDaohuoCount(entity.getCount());
                lvRukumingxi.setCreateBy(SecurityUtils.getUsername());
                lvRukumingxi.setType(BusinessConstants.TYPR_CHUKU);
                lvRukumingxi.setCreateTime(new Date());
                lvRukumingxi.setRukudate(new Date());
                lvRukumingxi.setSampleApplyId(entity.getId());
                lvRukumingxiMapper.insert(lvRukumingxi);
            }
        }


        // 如果入库 审核通过 加库存 增加入库明细
        if (BusinessConstants.SAMPLE_ASSESS_STATUS_IN_PASS.equals(entity.getStatus())) {
            if(StringUtils.isNotBlank(entity.getGoodsId())){
                GoodsOrderIn goodsOrderIn = goodsOrderInMapper.selectById(entity.getGoodsId());
                goodsOrderIn.setCount(goodsOrderIn.getCount().add(entity.getCount()));
                goodsOrderIn.setRemainderCount(goodsOrderIn.getRemainderCount().add(entity.getCount()));
                // 插入出库记录
                LvRukumingxi lvRukumingxi = new LvRukumingxi();
                lvRukumingxi.setId(IdUtils.fastUUID());
                lvRukumingxi.setGoodsId(goodsOrderIn.getId());
                lvRukumingxi.setCode(goodsOrderIn.getCode());
                lvRukumingxi.setUnit(goodsOrderIn.getUnit());
                lvRukumingxi.setDaohuoCount(entity.getCount());
                lvRukumingxi.setCreateBy(SecurityUtils.getUsername());
                lvRukumingxi.setType(BusinessConstants.TYPE_RUKU);
                lvRukumingxi.setCreateTime(new Date());
                lvRukumingxi.setRukudate(new Date());
                lvRukumingxi.setSampleApplyId(entity.getId());
                lvRukumingxiMapper.insert(lvRukumingxi);
            }
        }



        return goodsSampleApplyMapper.update(entity);
    }







    /**
     * 批量删除质检样品申请
     *
     * @param ids 需要删除的质检样品申请主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsSampleApplyMapper.deleteByIds(ids);
    }

    /**
     * 删除质检样品申请信息
     *
     * @param id 质检样品申请主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsSampleApplyMapper.deleteById(id);
    }
}
